package com.mingqi.enetconnected.primary.mytask.activity;

import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseActivity;
import com.mingqi.enetconnected.primary.mytask.adapter.LootAssignmentAdapter;
import com.mingqi.enetconnected.util.DeviceUtils;
import com.mingqi.enetconnected.util.hint.ToastUtils;
import com.mingqi.enetconnected.util.resource.ResourceUtils;
import com.mingqi.enetconnected.view.CustomLoadMoreView;
import com.mingqi.enetconnected.view.Dialog.LootAssignmentDialog;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import q.rorbin.badgeview.QBadgeView;

public class LootAssignmentActivity extends BaseActivity {

    @BindView(R.id.rv_loot_assignment)
    RecyclerView mRvLootAssignment;
    @BindView(R.id.srl_assignment)
    SwipeRefreshLayout mSrlAssignment;
    private View mEmptyView;
    private View mEmptyLoadView;
    private LootAssignmentAdapter mAdapter;

    private QBadgeView mQBadgeView;
    private LootAssignmentDialog mAssignmentDialog;
    private Disposable mRefreshDisposable;
    private Disposable mLoadMoreDisposable;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_loot_assignment;
    }

    @Override
    protected void initView() {

        setTitleName(ResourceUtils.getString(R.string.loot_assignment_pool));
        setRightImage(R.drawable.ic_bar_message, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ToastUtils.tMessage("点击了消息");
                startActivity(NotificationMessageActivity.class);

            }
        });

        mQBadgeView = new QBadgeView(this);
        mQBadgeView.bindTarget(mIvRightMore)
                .setBadgeNumber(9)
                .setBadgeGravity(Gravity.END | Gravity.TOP)
                .setBadgeTextSize(12,true)
                .setGravityOffset(0f, 0f, true);
    }

    @Override
    protected void initData() {

        initList();
    }

    private List<String> strings = new ArrayList<>();

    private void initList() {

        mSrlAssignment.setColorSchemeColors(mContext.getResources().getColor(R.color.colorPrimary));
        mRvLootAssignment.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
//        mRvLootAssignment.setHasFixedSize(true);
        mRvLootAssignment.addItemDecoration(
                new HorizontalDividerItemDecoration.Builder(this)
                        .colorResId(R.color.space)
                        .size(DeviceUtils.dp2px(mContext,10))
                        .build());
//        mRvLootAssignment.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST, R.drawable.space_list_large));
        mEmptyView = getLayoutInflater().inflate(R.layout.item_load_empty, (ViewGroup) mRvLootAssignment.getParent(), false);
        mEmptyLoadView = getLayoutInflater().inflate(R.layout.item_load_empty_loading, (ViewGroup) mRvLootAssignment.getParent(), false);

        mEmptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mAdapter.setEmptyView(mEmptyLoadView);
                refreshData();
            }
        });


        mAdapter = new LootAssignmentAdapter(strings);
        mRvLootAssignment.setAdapter(mAdapter);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
//        mAdapter.setEmptyView(R.layout.item_load_empty_loading, (ViewGroup) mRvLootAssignment.getParent());
//        mAdapter.disableLoadMoreIfNotFullPage();

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {

                ToastUtils.tMessage("加载更多");
                getMoreData();
            }
        }, mRvLootAssignment);

        mSrlAssignment.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                ToastUtils.tMessage("刷新");

                refreshData();
            }
        });

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                int id = view.getId();
                if (id == R.id.tv_loot_assignment) {

                    if(mAssignmentDialog == null) {

                        mAssignmentDialog = new LootAssignmentDialog(mContext);
                        mAssignmentDialog.setCancelable(false);
                        mAssignmentDialog.setCanceledOnTouchOutside(false);
                        mAssignmentDialog.show();
                    } else {

                        mAssignmentDialog.show();
                    }

                } else {

                    ToastUtils.tMessage("点击了其他");
                }
            }
        });

        mSrlAssignment.setRefreshing(true);
        refreshData();

//        mAdapter.setEmptyView(mEmptyView);
    }

    private void refreshData() {

        Observable.create(new ObservableOnSubscribe<List<String>>() {
            @Override
            public void subscribe(ObservableEmitter<List<String>> emitter) throws Exception {

                SystemClock.sleep(2000L);
                for (int i = 0; i < 5; i++) {

                    strings.add("刷新的数据的数据");
                }

                emitter.onNext(strings);
            }
        }).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {

                mRefreshDisposable = disposable;
                mSrlAssignment.setRefreshing(true);
            }
        }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<String>>() {
                    @Override
                    public void accept(List<String> strings) throws Exception {

                        mAdapter.setNewData(strings);

                        mAdapter.setEnableLoadMore(true);
                        mSrlAssignment.setRefreshing(false);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }

    private void getMoreData() {

        Observable.create(new ObservableOnSubscribe<List<String>>() {
            @Override
            public void subscribe(ObservableEmitter<List<String>> emitter) throws Exception {

                SystemClock.sleep(2000L);

                for (int i = 0; i < 10; i++) {

                    strings.add("更多的数据");
                }
                emitter.onNext(strings);

            }
        }).subscribeOn(Schedulers.io())
          .doOnSubscribe(new Consumer<Disposable>() {
              @Override
              public void accept(Disposable disposable) throws Exception {

                  mLoadMoreDisposable = disposable;
              }
          })
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<List<String>>() {
            @Override
            public void accept(List<String> strings) throws Exception {

                mAdapter.addData(strings);
                mAdapter.loadMoreComplete();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(mRefreshDisposable != null && !mRefreshDisposable.isDisposed()) {

            mRefreshDisposable.dispose();
        }

        if(mLoadMoreDisposable != null && !mLoadMoreDisposable.isDisposed()) {

            mLoadMoreDisposable.dispose();
        }
    }
}
